package com.hmall.trade.listener;

import com.hmall.api.client.ItemClient;
import com.hmall.trade.constants.MQConstants;
import com.hmall.trade.service.IOrderDetailService;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class PayStatusListener {

   private final IOrderService orderService;

   private final ItemClient itemClient;

   private final IOrderDetailService orderDetailService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange(value = "pay.direct",type = "direct"),
            key = "pay.success"
    ))
    public void payStatusListener(Long id) {
        orderService.markOrderPaySuccess(id);
        log.info("执行了payStatusListener方法，订单号：{}",id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.DELAY_ORDER_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(value = MQConstants.DELAY_EXCHANGE_NAME,delayed = "true",type = "direct"),
            key = MQConstants.DELAY_ORDER_KEY
    ))

    public void orderStatusListener(Long orderId) {
        orderService.handleOrderTimeout(orderId);

        log.info("执行了orderStatusListener方法，订单号：{}",orderId);
    }
}
